package com.hollysys.hollimsa.biz.istio.gateway.controller;


import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayDTO;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayDictDTO;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayVO;
import com.hollysys.hollimsa.biz.istio.gateway.service.GatewayService;
import com.hollysys.hollimsa.common.result.AjaxObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.List;

@RestController
@RequestMapping(value = "/serviceMesh")
@Validated
@Api(tags="istio 使能组件网关")
public class GatewayController {

    @Autowired
    private GatewayService gatewayService;

    /**
     * 增加网关
     * @param gateway 网关信息
     * @return  创建状态
     */
    @ApiOperation(value = "添加网关",notes = "添加网关")
    @PostMapping(value = "/gateway")
    public AjaxObject<Boolean> addGateway(@RequestBody @Validated GatewayVO gateway) {
        return AjaxObject.customOk("", gatewayService.addGateway(gateway));
    }


    /**
     * 网关列表
     * @return  网关列表
     */
    @ApiOperation(value = "网关列表",notes = "网关列表")
    @GetMapping(value = "/gateway")
    public AjaxObject<List<GatewayDTO>> listGateway() {
        return AjaxObject.customOk("", gatewayService.listGateway());
    }


    /**
     * 更改网关
     * @param gateway 网关信息
     * @return  创建状态
     */
    @ApiOperation(value = "更改网关",notes = "更改网关")
    @PutMapping(value = "/gateway")
    public AjaxObject<Boolean> editGateway(@RequestBody @Validated GatewayVO gateway) {
        return AjaxObject.customOk("", gatewayService.editGateway(gateway));
    }


    /**
     * 删除网关
     * @param name 网关名称
     * @return  创建状态
     */
    @ApiOperation(value = "删除网关",notes = "删除网关")
    @ApiImplicitParams({
        @ApiImplicitParam(name="name",value="网关名称",dataType="string", paramType = "query",example="istio-gateway-xxx")})
    @DeleteMapping("/gateway/{name}")
    public AjaxObject<Boolean> deleteistioGateWay(@PathVariable @NotEmpty(message="网关名称不能为空") String name) {
        return AjaxObject.customOk("", gatewayService.deleteGateway(name));
    }

    /**
     * 网关级联下拉
     */
    @ApiOperation(value = "网关级联下拉",notes = "网关级联下拉")
    @GetMapping("/gateway/gatewayDropDownData")
    public AjaxObject<List<GatewayDictDTO>> gatewayDropDownData() {
        return AjaxObject.customOk("", gatewayService.gatewayDropDownData());
    }

}
